//它模仿 css 长度系统，目前支持三种单位，包括 px、vw、vh
import 'package:flutter/material.dart';
import 'dart:ui';

class Dimension {
  static var _modulus;
  static var _dpWidth;
  static var _dpHeight;

  factory Dimension(BuildContext context){
    final basewidth = 375;

    _dpWidth = MediaQuery.of(context).size.width;
    _dpHeight = MediaQuery.of(context).size.height;
    final pxWidth = window.physicalSize.width;
    final pxHeight = window.physicalSize.height;
    final density = MediaQuery.of(context).devicePixelRatio;

    _modulus = (pxWidth/basewidth)/density; //系数

    final Dimension instance = Dimension._internal();
    return instance;
  }

  toPx(num px){
    return px*_modulus;
  }

  toVw(num percent,[width]){
    //传入的width要转化为dp
    final w = width != null?width*_modulus:_dpWidth;
    return (percent/100)*w;
  }

  toVh(num percent){
    return (percent/100)*_dpHeight;
  }

  Dimension._internal();
}